package de.kiltz.foo.mvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import de.kiltz.foo.domain.Foo;
import de.kiltz.foo.service.FooService;

public class ListeController extends SimpleFormController 
{
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    
    private FooService fooService;
    
    public FooService getFooService() {
		return fooService;
	}

	public void setFooService(FooService fooService) {
		this.fooService = fooService;
	}

	public ListeController()
    {
    	setCommandClass(ListeForm.class);
    	setCommandName("listeAuswahl");
    	
    }

	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ListeForm o = (ListeForm)request.getSession().getAttribute("listeAuswahl");
		if ( o == null)
		{
			o = new ListeForm();
		}
		return o;
	}


	public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,
    		Object command, BindException errors) throws Exception {

    	
        Map<String, Object> daten = new HashMap<String, Object>();
        ListeForm formi = (ListeForm)command;
        logger.info("suche nach " + formi.getSuch() + "...");

        try {
			List<Foo> liste = fooService.getByText(formi.getSuch());
	        logger.info("Finde Foos " + liste.size());
	        daten.put("info", liste.size()+" Foos gefunden. ");
	        daten.put("liste", liste);
	        
		} catch (Exception e) {
	        daten.put("info", e.getMessage());
			e.printStackTrace();
		}
        request.setAttribute("listeAuswahl", formi);
         
        return new ModelAndView(getSuccessView(), "daten", daten); 
        
    }
	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
        Map<String, Object> daten = new HashMap<String, Object>();
        
        logger.info("in showForm");
        String such = "";
        Object o = request.getSession().getAttribute("listeAuswahl");
        if (o != null && o instanceof ListeForm)
        {
        	such = ((ListeForm)o).getSuch();
            logger.info("habe ListeForm gefunden "+such);
        }
        else
        {
        	o = new ListeForm();
            logger.info("habe KEIN ListeForm gefunden! ");
        }
        request.setAttribute("listeAuswahl", o);
		List<Foo> liste = fooService.getByText(such);
        logger.info("Finde Foos " + liste.size());
        daten.put("info", liste.size()+" Foos gefunden. ");
        daten.put("liste", liste);
		return new ModelAndView(getSuccessView(), "daten", daten);
	}


}
